package com.ruoyi.bantong.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.bantong.domain.*;
import com.ruoyi.bantong.mapper.*;
import com.ruoyi.bantong.service.IBtUserAccountRecordService;
import com.ruoyi.common.core.domain.entity.BtUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bantong.service.IBtPlacecardGroupService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 红包包组Service业务层处理
 * 
 * @author laohei
 * @date 2021-03-26
 */
@Service
public class BtPlacecardGroupServiceImpl implements IBtPlacecardGroupService 
{
    @Autowired
    private BtPlacecardGroupMapper btPlacecardGroupMapper;

    @Autowired
    private BtPlacecardConfigMapper configMapper;

    @Autowired
    private BtUserLevelMapper levelMapper;
    @Autowired
    private BtUserMapper btUserMapper;
    @Autowired
    private IBtUserAccountRecordService accountRecordService;
    @Autowired
    private BtUserTicketMapper ticketMapper;
    @Autowired
    private BtPlacecardApplyMapper btPlacecardApplyMapper;

    /**
     * 查询红包包组
     * 
     * @param id 红包包组ID
     * @return 红包包组
     */
    @Override
    public BtPlacecardGroup selectBtPlacecardGroupById(Integer id)
    {
        return btPlacecardGroupMapper.selectBtPlacecardGroupById(id);
    }

    /**
     * 查询红包包组列表
     * 
     * @param btPlacecardGroup 红包包组
     * @return 红包包组
     */
    @Override
    public List<BtPlacecardGroup> selectBtPlacecardGroupList(BtPlacecardGroup btPlacecardGroup)
    {
        return btPlacecardGroupMapper.selectBtPlacecardGroupList(btPlacecardGroup);
    }

    /**
     * 查询红包包组 将要获得的金额
     *
     * @param level 等级
     * @return 红包包组
     */
    @Override
    public BtPlacecardGroup getBtPlacecardGroupByLevel(Integer level,Integer userId) throws Exception{

        BtPlacecardConfig btPlacecardConfig = configMapper.selectBtPlacecardConfigById(level);
        BtUserLevel btUserLevel = levelMapper.selectBtUserLevelByLevel(level);
        BigDecimal winCount = new BigDecimal(btPlacecardConfig.getWinCount()); // 中奖人数
        BigDecimal playCount = new BigDecimal(btPlacecardConfig.getPlayCount()); //参加人数
        BigDecimal lossCount = playCount.subtract(winCount); // 未中奖人数
        BigDecimal accountMoney = new BigDecimal(btUserLevel.getAccountMoney()); //中签后奖励版通数
        BigDecimal losGotMoney = new BigDecimal(btUserLevel.getLosGotMoney()); // 未中奖获得金额
        BigDecimal winGotNum = new BigDecimal(btUserLevel.getWinGotNum()); // 中奖后获得奖券数量
        //判断余额
        BtUser btUser = btUserMapper.selectBtUserById(userId);
        BigDecimal btBean = btUser.getBtBean(); //金豆余额
        if(winCount.multiply(accountMoney).compareTo(btBean) == 1){
            throw new CustomException("用户金豆不足！");
        }
        BtPlacecardGroup btPlacecardGroup = new BtPlacecardGroup();
        btPlacecardGroup.setConvertMoney(winCount.multiply(accountMoney).intValue());
        btPlacecardGroup.setGotBtBean(lossCount.multiply(losGotMoney).intValue());
        btPlacecardGroup.setGotBtTicket(winCount.multiply(winGotNum).intValue());
        btPlacecardGroup.setLevel(level);
        return btPlacecardGroup;
    }

    /**
     * 新增红包包组
     * 
     * @param btPlacecardGroup 红包包组
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBtPlacecardGroup(BtPlacecardGroup btPlacecardGroup)
    {
        Integer convertMoney = btPlacecardGroup.getConvertMoney();//用户获得积分
        Integer gotBtBean = btPlacecardGroup.getGotBtBean(); //用户获得金豆
        Integer gotBtTicket = btPlacecardGroup.getGotBtTicket();// 用户获得奖券
        Integer level = btPlacecardGroup.getLevel();
        //扣除余额
        accountRecordService.updataUserAccount(btPlacecardGroup.getUserId(),BigDecimal.valueOf(convertMoney),BigDecimal.ZERO,
                null,"bt_placecard_pool","out",6);
        //奖励一定金豆
        accountRecordService.updataUserAccount(btPlacecardGroup.getUserId(),BigDecimal.valueOf(gotBtBean),BigDecimal.ZERO,
                null,"bt_placecard_pool",
                "in",4);
        //增加版通
        accountRecordService.updataUserAccount(btPlacecardGroup.getUserId(),BigDecimal.ZERO,BigDecimal.valueOf(convertMoney),
                null,"bt_placecard_apply","in",7);
        for (int i = 0; i < gotBtTicket; i++) {
            //奖励奖券
            BtUserTicket btUserTicket = new BtUserTicket();
            btUserTicket.setUserId(btPlacecardGroup.getUserId());
            btUserTicket.setTotalFee(1500);
            btUserTicket.setCreateTime(DateUtils.getNowDate());
            ticketMapper.insertBtUserTicket(btUserTicket);
        }
        //增加审核记录
        BtPlacecardApply apply = new BtPlacecardApply();
        BtPlacecardConfig btPlacecardConfig = configMapper.selectBtPlacecardConfigById(level);
        apply.setOutTime(DateUtils.dateAddMinutes(null,btPlacecardConfig.getCheckInterval() * 60));
        apply.setConvertMoney(convertMoney);
        apply.setLevel(level);
        apply.setUserId(btPlacecardGroup.getUserId());
        apply.setStatus(1);
        apply.setCreateBy(SecurityUtils.getUsername());
        apply.setCreateTime(DateUtils.getNowDate());
        btPlacecardApplyMapper.insertBtPlacecardApply(apply);
        btPlacecardGroup.setCreateBy(SecurityUtils.getUsername());
        btPlacecardGroup.setCreateTime(DateUtils.getNowDate());
        return btPlacecardGroupMapper.insertBtPlacecardGroup(btPlacecardGroup);
    }

    /**
     * 修改红包包组
     * 
     * @param btPlacecardGroup 红包包组
     * @return 结果
     */
    @Override
    public int updateBtPlacecardGroup(BtPlacecardGroup btPlacecardGroup)
    {
        btPlacecardGroup.setUpdateTime(DateUtils.getNowDate());
        return btPlacecardGroupMapper.updateBtPlacecardGroup(btPlacecardGroup);
    }

    /**
     * 批量删除红包包组
     * 
     * @param ids 需要删除的红包包组ID
     * @return 结果
     */
    @Override
    public int deleteBtPlacecardGroupByIds(Integer[] ids)
    {
        return btPlacecardGroupMapper.deleteBtPlacecardGroupByIds(ids);
    }

    /**
     * 删除红包包组信息
     * 
     * @param id 红包包组ID
     * @return 结果
     */
    @Override
    public int deleteBtPlacecardGroupById(Integer id)
    {
        return btPlacecardGroupMapper.deleteBtPlacecardGroupById(id);
    }
}
